export default class DownloadJson {
    private  _data: cc.JsonAsset = null;
    private  _N_data: cc.JsonAsset = null;
    private  readonly _url: string ='http://112.74.172.176:8051/weifen/testLoadJson/version100.json';
    private  readonly_NdataUrl: string = 'framework/data'; 

     async load_N_Data(): Promise<cc.JsonAsset> {
        return new Promise((resolve, reject) => {

            if (!this._N_data) {
                cc.resources.load<cc.JsonAsset>(this.readonly_NdataUrl, cc.JsonAsset, (err: Error, asset: cc.JsonAsset) => {
                    if (err) {
                        return reject(err)
                    } else {
                        this._N_data = asset.json
                        resolve(this._N_data)
                    }
                })
            } else {
                resolve(this._N_data)
            }
        })
    }

    async getData(): Promise<cc.JsonAsset> { 
        return new Promise((resolve, reject)=>{
            if (!this._data) {
                cc.assetManager.loadRemote(this._url,cc.JsonAsset,(err:Error, asset: cc.JsonAsset)=>{
                    if(err){
                        return reject(err);
                    } else {
                        this._data = asset.json;
                        return resolve(this._data);
                    }
                });
            } else{
                return resolve(this._data);
            }
        })
    }

    httpPost() {
        return new Promise((resolve,reject)=>{
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                cc.log('xhr.readyState='+xhr.readyState+'  xhr.status='+xhr.status);
                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
                    var respone = xhr.responseText;
                    console.log(respone);
                    resolve(respone);
                }
            };
            var url_temp = "http://112.74.172.176:8051/weifen/testLoadJson/version100.json";
            xhr.open("GET", url_temp, true);
           
    
            // note: In Internet Explorer, the timeout property may be set only after calling the open()
            // method and before calling the send() method.
            xhr.timeout = 5000;// 5 seconds for timeout
            // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
            xhr.send();
        })
        
    }

}